package com.intct.hbase.ddl.table;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-11-04 09:23
 */
public class TruncateTable {
    public static void main(String[] args) throws IOException {
        // 建立连接
        Connection conn = ConnectionFactory.createConnection();

        // 获取Aadmin实例
        Admin admin = conn.getAdmin();

        // 获取TableName实例
        TableName tableName = TableName.valueOf("intct86", "t2");

        try {
            // 判断表是否存在，存在则清空
            if (admin.tableExists(tableName)) {
                // 禁用表
                admin.disableTable(tableName);

                // 清空表
                admin.truncateTable(tableName, true);
            }
        } catch (IOException e) {
            System.out.println("删除表失败！");
        } finally {
            // 关闭资源
            if (admin != null) {
                admin.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }
}
